import Vue from "vue";
import Vuex from "vuex";
import date from "@/tool/date";
Vue.use(Vuex);
function sessionStg(state) {
  window.sessionStorage.setItem("state", JSON.stringify(state));
}
export default new Vuex.Store({
  state: {
    userInfo: {
      img_head: "",
      username: "",
      order: {},
      login_time: "",
      mobile: "",
      money: ""
    },
    isLogin: false, // 是否登录
    isValidate: false, //是否实名验证
    isSetPayPsw: false, //是否设置支付密码
    isCard: false, // 是否添加了银行卡
    isShowNav: true, // 是否显示底部导航栏, 移动端
    activeHeaderMenu: "home", // active底部导航栏
    currentRouterName: "首页", // 当前router的name
    listType: 1, //商品展示的方式，1是格子，2是列表
    order: {
      id: "",
      address: "",
      order_num: ""
    } //订单的id和订单的地址
  },
  mutations: {
    changeLogin(state, info) {
      state.isLogin = info;
      sessionStg(state);
    },
    changeValidate(state, info) {
      let validate = null;
      if (info === 1) {
        validate = true;
      } else {
        validate = false;
      }
      state.isValidate = validate;
      sessionStg(state);
    },
    changeCard(state, info) {
      let validate = null;
      if (info === "YES") {
        validate = true;
      } else {
        validate = false;
      }
      state.isCard = validate;
      sessionStg(state);
    },
    changeSetPay(state, info) {
      let validate = null;
      if (info === "YES") {
        validate = true;
      } else {
        validate = false;
      }
      state.isSetPayPsw = validate;
      sessionStg(state);
    },
    changeIsShowNav(state, isShowNav) {
      state.isShowNav = isShowNav;
      sessionStg(state);
    },
    changeActiveMenu(state, routerName) {
      state.activeHeaderMenu = routerName;
      sessionStg(state);
    },
    changeCurrentRouterName(state, routerName) {
      state.currentRouterName = routerName;
      sessionStg(state);
    },
    changeListType(state, ListType) {
      state.listType = ListType;
      sessionStg(state);
    },
    changeOrder(state, info) {
      Object.assign(state.order, info);
      sessionStg(state);
    },
    changeUserImg(state, info) {
      state.userInfo.img_head = info;
      sessionStg(state);
    },
    changeUsername(state, info) {
      state.userInfo.username = info;
      sessionStg(state);
    },
    changeUserOrder(state, info) {
      state.userInfo.order = info;
      sessionStg(state);
    },
    changeLoginTime(state, info) {
      state.userInfo.login_time = date.date(info * 1000);
      sessionStg(state);
    },
    changeUserMobile(state, info) {
      state.userInfo.mobile = info;
      sessionStg(state);
    },
    changeUserMoney(state, info) {
      state.userInfo.money = info;
      sessionStg(state);
    }
  },
  actions: {
    changeLogin(context, info) {
      context.commit("changeLogin", info);
    },
    changeValidate(context, info) {
      context.commit("changeValidate", info);
    },
    changeCard(context, info) {
      context.commit("changeCard", info);
    },
    changeSetPay(context, info) {
      context.commit("changeSetPay", info);
    },
    changeIsShowNav(context, isShowNav) {
      context.commit("changeIsShowNav", isShowNav);
    },
    changeActiveMenu(context, routerName) {
      context.commit("changeActiveMenu", routerName);
    },
    changeCurrentRouterName(context, routerName) {
      context.commit("changeCurrentRouterName", routerName);
    },
    changeListType(context, ListType) {
      context.commit("changeListType", ListType);
    },
    changeOrder(context, info) {
      context.commit("changeOrder", info);
    },
    changeUserImg(context, info) {
      context.commit("changeUserImg", info);
    },
    changeUsername(context, info) {
      context.commit("changeUsername", info);
    },
    changeUserOrder(context, info) {
      context.commit("changeUserOrder", info);
    },
    changeLoginTime(context, info) {
      context.commit("changeLoginTime", info);
    },
    changeUserMobile(context, info) {
      context.commit("changeUserMobile", info);
    },
    changeUserMoney(context, info) {
      context.commit("changeUserMoney", info);
    }
  }
});
